package com.ruoyi.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.mapper.VideoRecoverStationMapper;
import com.ruoyi.domain.VideoRecoverStation;
import com.ruoyi.service.IVideoRecoverStationService;
import com.ruoyi.mapper.PartyclassMapper;
import com.ruoyi.domain.Partyclass;

/**
 * 视频回收站Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-09-12
 */
@Service
public class VideoRecoverStationServiceImpl extends ServiceImpl<VideoRecoverStationMapper, VideoRecoverStation> implements IVideoRecoverStationService
{
    @Autowired
    private VideoRecoverStationMapper videoRecoverStationMapper;

    @Autowired
    private PartyclassMapper partyclassMapper;

    /**
     * 查询视频回收站
     * 
     * @param id 视频回收站主键
     * @return 视频回收站
     */
    @Override
    public VideoRecoverStation selectVideoRecoverStationById(Long id)
    {
        return videoRecoverStationMapper.selectVideoRecoverStationById(id);
    }

    /**
     * 查询视频回收站列表
     * 
     * @param videoRecoverStation 视频回收站
     * @return 视频回收站
     */
    @Override
    public List<VideoRecoverStation> selectVideoRecoverStationList(VideoRecoverStation videoRecoverStation)
    {
        return videoRecoverStationMapper.selectVideoRecoverStationList(videoRecoverStation);
    }

    /**
     * 新增视频回收站
     * 
     * @param videoRecoverStation 视频回收站
     * @return 结果
     */
    @Override
    public int insertVideoRecoverStation(VideoRecoverStation videoRecoverStation)
    {
        return videoRecoverStationMapper.insertVideoRecoverStation(videoRecoverStation);
    }

    /**
     * 修改视频回收站
     * 
     * @param videoRecoverStation 视频回收站
     * @return 结果
     */
    @Override
    public int updateVideoRecoverStation(VideoRecoverStation videoRecoverStation)
    {
        return videoRecoverStationMapper.updateVideoRecoverStation(videoRecoverStation);
    }

    /**
     * 批量删除视频回收站
     * 
     * @param ids 需要删除的视频回收站主键
     * @return 结果
     */
    @Override
    public int deleteVideoRecoverStationByIds(Long[] ids)
    {
        return videoRecoverStationMapper.deleteVideoRecoverStationByIds(ids);
    }

    /**
     * 删除视频回收站信息
     * 
     * @param id 视频回收站主键
     * @return 结果
     */
    @Override
    public int deleteVideoRecoverStationById(Long id)
    {
        return videoRecoverStationMapper.deleteVideoRecoverStationById(id);
    }

    /**
     * 恢复视频数据
     * 
     * @param recoverIds 需要恢复的视频回收站ID数组
     * @return 恢复的记录数
     */
    @Override
    public int recoverVideoData(Long[] recoverIds) {
        int count = 0;
        for (Long recoverId : recoverIds) {
            // 查询回收站记录
            VideoRecoverStation recoverStation = this.getById(recoverId);
            if (recoverStation != null) {
                // 恢复到partyclass表
                Partyclass partyclass = new Partyclass();
                // 复制字段
                partyclass.setName(recoverStation.getName());
                partyclass.setVideoOrigin(recoverStation.getVideoOrigin());
                partyclass.setFile(recoverStation.getFile());
                partyclass.setAuthor(recoverStation.getAuthor());
                partyclass.setCoverImg(recoverStation.getCoverImg());
                partyclass.setPubdate(recoverStation.getPubdate());
                if (recoverStation.getX() != null)
                    partyclass.setX(recoverStation.getX().doubleValue());
                if (recoverStation.getY() != null)
                    partyclass.setY(recoverStation.getY().doubleValue());
                if (recoverStation.getType() != null)
                    partyclass.setType(recoverStation.getType());
                if (recoverStation.getSortOrder() != null)
                    partyclass.setSortOrder(recoverStation.getSortOrder());
                
                // 如果排序字段为空，设置默认排序值
                if (partyclass.getSortOrder() == null) {
                    int maxSortOrder = partyclassMapper.selectMaxSortOrderByType(recoverStation.getType());
                    partyclass.setSortOrder(maxSortOrder + 1);
                }
                
                // 插入到partyclass表
                partyclassMapper.insertPartyclass(partyclass);
                
                // 删除回收站记录
                this.removeById(recoverId);
                count++;
            }
        }
        return count;
    }

    /**
     * 删除30天前的视频回收站数据
     * 
     * @param beforeTime 时间点
     * @return 删除的记录数
     */
    @Override
    public int deleteVideoRecoverDataBefore(String beforeTime) {
        return videoRecoverStationMapper.deleteVideoRecoverDataBefore(beforeTime);
    }
}
